import { MatrixUtil } from "../utils/MatrixUtil";
import { WebglBase } from "./WebglBase";
import { Render } from "./Render";

/*
 * @Author: Snow
 * @Date: 2021-08-31 15:04:00
 * @Description: 
 */
export class WebglCube extends WebglBase{
    public override initGLSL():void{
        super.initGLSL();
        return;
        this.vsSource = `
            attribute vec4 aVertexPosition;
            attribute vec4 aVertexColor;

            uniform mat4 uModelViewMatrix;
            uniform mat4 uProjectionMatrix;
            uniform mat4 uViewMatrix;

            varying lowp vec4 vColor;

            void main() {
                gl_Position = uProjectionMatrix * uViewMatrix * uModelViewMatrix * aVertexPosition;
                vColor = aVertexColor;
            }
        `;
        
        this.fsSource = `
            varying lowp vec4 vColor;
            void main() {
                gl_FragColor = vColor;
            }
        `;
    }
    public override updateMatrix():void{
        const gl = Render.GL;
        const mat4 = (window as any).glMatrix.mat4;
        const fieldOfView = Math.PI * 0.5;
		const aspect = Render.Canvas.clientWidth / Render.Canvas.clientHeight;
		const zNear = 0.1;
		const zFar = 50;
		let projectionMatrix = mat4.create();
        let viewMatrix = mat4.create();
        projectionMatrix = MatrixUtil.perspective(projectionMatrix,fieldOfView,aspect,zNear,zFar);
		
		// mat4.perspective(projectionMatrix,
        //            fieldOfView,
        //            aspect,
        //            zNear,
        //            zFar);
        
        let modelViewMatrix = mat4.create();
        
        //modelViewMatrix.moveMatrix(0,0,20);
        //缩放
        modelViewMatrix = MatrixUtil.multiplyMatrices(modelViewMatrix,MatrixUtil.scaleMatrix(0.5,0.5,0.5));
        //旋转
        modelViewMatrix = MatrixUtil.multiplyMatrices(modelViewMatrix,MatrixUtil.rotateZMatrix(this.rotation));
        modelViewMatrix = MatrixUtil.multiplyMatrices(modelViewMatrix,MatrixUtil.rotateXMatrix(this.rotation * 0.8));
        // mat4.rotate(modelViewMatrix,  // destination matrix
        //     modelViewMatrix,  // matrix to rotate
        //     this.rotation,   // amount to rotate in radians
        //     [0, 0, 1] // axis to rotate around
        // );
        //位移
        let w = 100;
        let h = 50;
        //modelViewMatrix = MatrixUtil.multiplyMatrices(modelViewMatrix,MatrixUtil.moveMatrix(w / Render.clientWidth,h / Render.clientHeight,0));
        modelViewMatrix = MatrixUtil.multiplyMatrices(modelViewMatrix,MatrixUtil.moveMatrix(0,0,0));
        // mat4.translate(modelViewMatrix,     // destination matrix
        //     modelViewMatrix,     // matrix to translate
        //     [-0.0, 0.0, -6.0]);  // amount to translate

        // mat4.rotate(modelViewMatrix,  // destination matrix
        //     modelViewMatrix,  // matrix to rotate
        //     this.rotation,   // amount to rotate in radians
        //     [0, 1, 1] // axis to rotate around
        // );
        //viewMatrix = MatrixUtil.scaleMatrix(4,4,4)
        viewMatrix = MatrixUtil.moveMatrix(2,1,-6);
        
        this.projectionMatrix = projectionMatrix;
        this.modelViewMatrix = modelViewMatrix;
        this.viewMatrix = viewMatrix;
        
    }
}